package utils;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Yu on 2017/12/19.
 */
public class ExportExcel {

    public void createExcel(String path) throws WriteException,IOException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
        Date date = new Date(System.currentTimeMillis());

        File f = new File(path);
        if (!f.exists()) {
            f.mkdirs();
        }

        String fileName = path+"gasExport_"+formatter.format(date)+".xls";
        File file = new File(fileName);//根据文件名创建一个文件对象
        file.createNewFile();
        System.out.println(file.getPath());
        OutputStream os=new FileOutputStream(file);
        //创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
        //创建要显示的具体内容
        Label formate = new Label(0,0,"图片ID");
        sheet.addCell(formate);
        Label floats = new Label(1,0,"识别结果");
        sheet.addCell(floats);
        Label integers = new Label(2,0,"校验结果");
        sheet.addCell(integers);
        Label booleans = new Label(3,0,"时间");
        sheet.addCell(booleans);
//        Label dates = new Label(4,0,"日期格式");
//        sheet.addCell(dates);
//
//        Label example = new Label(0,1,"数据示例");
//        sheet.addCell(example);
//        //浮点数据
//        Number number = new Number(1,1,3.1415926535);
//        sheet.addCell(number);
//        //整形数据
//        Number ints = new Number(2,1,15042699);
//        sheet.addCell(ints);
//        Boolean bools = new Boolean(3,1,true);
//        sheet.addCell(bools);
//        //日期型数据
//        Calendar c = Calendar.getInstance();
//        Date date = c.getTime();
//        WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT1);
//        DateTime dt = new DateTime(4,1,date,cf1);
//        sheet.addCell(dt);
        //由此获取数据
        getExcelData();
        for (int i = 1; i < 60000; i++) {
            Label example = new Label(0,i,i+"");
            sheet.addCell(example);

            Label example1 = new Label(1,i,"123456");
            sheet.addCell(example1);

            Label example2 = new Label(2,i,"正确");
            sheet.addCell(example2);

            Label example3 = new Label(3,i,"2017-10-25");
            sheet.addCell(example3);
        }
        //把创建的内容写入到输出流中，并关闭输出流
        workbook.write();
        workbook.close();
        os.close();

    }


    public void getExcelData() {


    }

}
